package org.mikrosopht.client;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.Widget;

public class MovieListPager extends Composite implements MouseListener {
  private static final String FIRST_IMG = MovieVoteConstants.IMAGE_PATH_URL + "/first.png";
  private static final String FIRST_DISABLED_IMG = MovieVoteConstants.IMAGE_PATH_URL + "/first-grey.png";
  private static final String PREV_IMG = MovieVoteConstants.IMAGE_PATH_URL + "/prev.png";
  private static final String PREV_DISABLED_IMG = MovieVoteConstants.IMAGE_PATH_URL + "/prev-grey.png";
  private static final String ALL_IMG = MovieVoteConstants.IMAGE_PATH_URL + "/show-all.png";
  private static final String SOME_IMG = MovieVoteConstants.IMAGE_PATH_URL + "/show-some.png";
  private static final String NEXT_IMG = MovieVoteConstants.IMAGE_PATH_URL + "/next.png";
  private static final String NEXT_DISABLED_IMG = MovieVoteConstants.IMAGE_PATH_URL + "/next-grey.png";
  private static final String LAST_IMG = MovieVoteConstants.IMAGE_PATH_URL + "/last.png";
  private static final String LAST_DISABLED_IMG = MovieVoteConstants.IMAGE_PATH_URL + "/last-grey.png";

  private Image firstImage = new Image(FIRST_IMG);
  private Image firstDisabledImage = new Image(FIRST_DISABLED_IMG);
  private Image prevImage = new Image(PREV_IMG);
  private Image prevDisabledImage = new Image(PREV_DISABLED_IMG);
  private Image allImage = new Image(ALL_IMG);
  private Image someImage = new Image(SOME_IMG);
  private Image nextImage = new Image(NEXT_IMG);
  private Image nextDisabledImage = new Image(NEXT_DISABLED_IMG);
  private Image lastImage = new Image(LAST_IMG);
  private Image lastDisabledImage = new Image(LAST_DISABLED_IMG);
  private HorizontalPanel pagerPanel = new HorizontalPanel();
  private MovieListPagerEventReceiver receiver;

  public enum Button { FIRST, PREVIOUS, ALL, NEXT, LAST };

  public MovieListPager(MovieListPagerEventReceiver receiver) {
    this.receiver = receiver;
    Image[] images = new Image[] { firstImage, prevImage, allImage, nextImage, lastImage };
    for (Image image : images) {
      image.addMouseListener(this);
      pagerPanel.add(image);
    }
    someImage.addMouseListener(this);
    initWidget(pagerPanel);
  }

  public void setDisabled(Button button) {
    switch (button) {
      case FIRST:
        pagerPanel.remove(firstImage);
        pagerPanel.insert(firstDisabledImage, 0);
        break;
      case PREVIOUS:
        pagerPanel.remove(prevImage);
        pagerPanel.insert(prevDisabledImage, 1);
        break;
      case ALL:
        pagerPanel.remove(allImage);
        pagerPanel.insert(someImage, 2);
        break;
      case NEXT:
        pagerPanel.remove(nextImage);
        pagerPanel.insert(nextDisabledImage, 3);
        break;
      case LAST:
        pagerPanel.remove(lastImage);
        pagerPanel.insert(lastDisabledImage, 4);
        break;
    }
  }

  public void setEnabled(Button button) {
    switch (button) {
      case FIRST:
        pagerPanel.remove(firstDisabledImage);
        pagerPanel.insert(firstImage, 0);
        break;
      case PREVIOUS:
        pagerPanel.remove(prevDisabledImage);
        pagerPanel.insert(prevImage, 1);
        break;
      case ALL:
        pagerPanel.remove(someImage);
        pagerPanel.insert(allImage, 2);
        break;
      case NEXT:
        pagerPanel.remove(nextDisabledImage);
        pagerPanel.insert(nextImage, 3);
        break;
      case LAST:
        pagerPanel.remove(lastDisabledImage);
        pagerPanel.insert(lastImage, 4);
        break;
    }
  }

  public void onMouseEnter(Widget sender) {
    sender.addStyleName("clickable");
  }
  public void onMouseLeave(Widget sender) {
    sender.removeStyleName("clickable");
  }
  public void onMouseMove(Widget sender, int x, int y) {
  }
  public void onMouseUp(Widget sender, int x, int y) {
  }
  public void onMouseDown(Widget sender, int x, int y) {
    Button clicked = null;
    if (sender == firstImage) {
      clicked = Button.FIRST;
    } else if (sender == prevImage) {
      clicked = Button.PREVIOUS;
    } else if (sender == allImage || sender == someImage) {
      clicked = Button.ALL;
    } else if (sender == nextImage) {
      clicked = Button.NEXT;
    } else if (sender == lastImage) {
      clicked = Button.LAST;
    }
    if (clicked != null) {
      receiver.onClick(clicked);
    }
  }
}
